package jgl.vajra.kingkong.dialogFragment;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;

import androidx.fragment.app.DialogFragment;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.databinding.DialogFragmentModifyContactBinding;
import jgl.vajra.kingkong.listener.ModifyContactListener;
import jgl.vajra.kingkong.widget.dialogfragment.BaseDialogFragment;
import jgl.vajra.kingkong.widget.utils.UIUtils;

public class ModifyContactDialogFragment extends BaseDialogFragment<DialogFragmentModifyContactBinding> {

    private String contactPerson;
    private String contactPhone;
    private ModifyContactListener listener;

    @Override
    public void initView(Context context, View rootView, AttributeSet attrs) {
        mBinding.setView(this);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomSheetEdit);

        mBinding.etName.setText(contactPerson);
        mBinding.etPhone.setText(contactPhone);

    }

    @Override
    public void initData(Context context) {
    }

    @Override
    public void initEvent(Context context) {

    }


    public int getLayoutRes() {
        return R.layout.dialog_fragment_modify_contact;
    }


    @Override
    public int getGravity() {
        return Gravity.CENTER;
    }


    public void setData(String contactPerson, String contactPhone) {
        this.contactPerson = contactPerson;
        this.contactPhone = contactPhone;
    }

    //通讯录

    public void onMailList() {
        Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(intent, 0);
    }


    /*  跳转联系人列表的回调函数  */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (data == null) {
                return;
            }
            //处理返回的data,获取选择的联系人信息
            Uri uri = data.getData();
            String[] contacts = getPhoneContacts(uri);
            mBinding.etName.setText(contacts != null ? contacts[0] : "");

            String phone = contacts != null ? contacts[1] : "";
            if (!TextUtils.isEmpty(phone)) {
                phone =phone.replace(" ", "");
            }
            mBinding.etPhone.setText(phone);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private String[] getPhoneContacts(Uri uri) {
        String[] contact = new String[2];
        //得到ContentResolver对象
        ContentResolver cr = UIUtils.getContext().getContentResolver();
        //取得电话本中开始一项的光标
        Cursor cursor = cr.query(uri, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            //取得联系人姓名
            int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            contact[0] = cursor.getString(nameFieldColumnIndex);
            //取得电话号码
            String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
            if (phone != null) {
                phone.moveToFirst();
                contact[1] = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                phone.close();
            }
            cursor.close();
        } else {
            return null;
        }
        return contact;
    }

    public void setModifyContactListener(ModifyContactListener listener) {
        this.listener = listener;
    }

    //确定
    public void onDetermine() {
        contactPerson = mBinding.etName.getText().toString().trim();
        contactPhone = mBinding.etPhone.getText().toString().trim();

        listener.onDetermine(contactPerson, contactPhone);
        dismiss();
    }

}
